/**
 * @author scylla
 * @version V1.0
 * @Project echo-java-base
 * @Title 线程优先级
 * @Description *
 * @Package concurrent.mthread.priority
 * @Since 2021/7/27 1:13 下午
 */
package concurrent.mthread.priority;

public class SimplePriorities implements Runnable {

    private int countDown = 5;
    private volatile double d;
    private int priority;

    public SimplePriorities(int priority) {
        this.priority = priority;
    }

    @Override
    public String toString() {
        return Thread.currentThread() + ":" + countDown;
    }

    @Override
    public void run() {
        Thread.currentThread().setPriority(priority);
        while (true) {
            //一段很消耗性能的并且可满足条件后可短暂中断的程序
            for (int i = 0; i < 100000; i++) {
                d += (Math.PI + Math.E) / (double) i;
                if (i % 1000 == 0)
                    //线程 让步
                    Thread.yield();

            }

            //打印自身
            System.out.println(this);

            //跳出当前线程
            if (--countDown == 0) return;
        }
    }
}
